package com.beiding.v2.grammar;

import com.beiding.v2.base.IUnit;
import com.beiding.v2.element.IExpression;

import java.util.List;

public class Case {

    private SqlBuilder sqlBuilder;

    public static When When(Object obj) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.caze().when().auto(obj);
        When when = new When();
        when.sqlBuilder = sqlBuilder;
        return when;
    }

    public When when(Object obj) {
        sqlBuilder.when().auto(obj);
        When when = new When();
        when.sqlBuilder = this.sqlBuilder;
        return when;
    }

    public static Case $(Object obj) {
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.caze().auto(obj);
        Case caze = new Case();
        caze.sqlBuilder = sqlBuilder;
        return caze;
    }

    public static When When(ExpressionBuilder expressionBuilder) {
        SqlBuilder builder = new SqlBuilder();
        expressionBuilder.build(builder);
        SqlBuilder sqlBuilder = new SqlBuilder();
        sqlBuilder.caze().when().auto(builder.createExpression());
        When when = new When();
        when.sqlBuilder = sqlBuilder;
        return when;
    }



    public static class When {

        private SqlBuilder sqlBuilder;

        private When() {
        }

        public Then then(Object obj) {
            sqlBuilder.then().auto(obj);
            Then then = new Then();
            then.sqlBuilder = this.sqlBuilder;
            return then;
        }

    }

    public static class Then {

        private SqlBuilder sqlBuilder;

        private Then() {
        }

        public When when(Object obj) {
            sqlBuilder.when().auto(obj);
            When when = new When();
            when.sqlBuilder = this.sqlBuilder;
            return when;
        }

        public Else elze(Object obj) {
            sqlBuilder.elze().auto(obj);
            Else elze = new Else();
            elze.sqlBuilder = this.sqlBuilder;
            return elze;
        }
    }

    public static class Else {

        private SqlBuilder sqlBuilder;

        private Else() {
        }

        public End end() {
            sqlBuilder.end();
            End end = new End();
            end.sqlBuilder = this.sqlBuilder;
            return end;
        }

    }

    public static class End implements IExpression {

        private SqlBuilder sqlBuilder;

        private boolean inner = false;

        private End() {

        }

        public IExpression as(String as) {
            return sqlBuilder.as(as).createExpression();
        }

        @Override
        public List<IUnit> getChildren() {
            return sqlBuilder.createExpression().getChildren();
        }

        @Override
        public boolean isInner() {
            return inner;
        }

        @Override
        public void setInner(boolean inner) {
            this.inner = inner;
        }
    }

}
